#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Queue* p)
{
	assert(p);
	p->head = p->tail = NULL;
	p->size = 0;
}

void QueueDestroy(Queue* p)
{
	assert(p);
	QNode* cur = p->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	p->head = p->tail = NULL;
	p->size = 0;
}

void QueuePush(Queue* p, QDataType x)
{
	assert(p);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->next = NULL;
	newnode->data = x;
	if (p->head == NULL)
	{
		p->head = p->tail = newnode;
	}
	else
	{
		p->tail->next = newnode;
		p->tail = newnode;
	}
	p->size++;
}

void QueuePop(Queue* p)
{
	assert(p);
	assert(!QueueEmpty(p));
	if (p->head == p->tail)
	{
		free(p->head);
		p->head = p->tail = NULL;
	}
	else
	{
		QNode* next = p->head->next;
		free(p->head);
		p->head = next;
	}
	p->size--;
}


QDataType QueueFront(Queue* p)
{
	assert(p);
	assert(!QueueEmpty(p));
	return p->head->data;
}

QDataType QueueBack(Queue* p)
{
	assert(p);
	assert(!QueueEmpty(p));
	return p->tail->data;
}

bool QueueEmpty(Queue* p)
{
	assert(p);
	return p->size == 0;
}

int QueueSize(Queue* p)
{
	assert(p);
	return p->size;
}